//
//  HomeAddFocusController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/17.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeAddFocusController.h"
#import "NavigationBarTitleView.h"
#import "HomeSerachController.h"

@interface HomeAddFocusController ()<UITableViewDelegate,UITableViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)PubulicNavSerachView*titleView;

/**listView*/
@property (nonatomic,strong)UITableView*listView;
/**headerView*/
@property (nonatomic,strong)UIView*headerView;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation HomeAddFocusController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xF5F7F5);
    [self createUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndNotClearColor];
    self.isShowLiftBack = NO;
}
-(void)createUI{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.titleView = self.titleView;
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 8.0f;
    }
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeAddFocusCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeAddFocusCell class])];
    cell.model = self.dataArray[indexPath.row];
    kWeakSelf(weakSelf);
    cell.tapBlock = ^{
        HomeAddFocusModel * seleModel = weakSelf.dataArray[indexPath.row];
        seleModel.isGuan = YES;
        weakSelf.dataArray[indexPath.row] = seleModel;
        [weakSelf.listView reloadData];
    };
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalHomePageBasseController * vc = [[PersonalHomePageBasseController alloc] init];
    vc.type = PersonalHomePageTA;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)searcheAction{
    HomeSerachController * vc = [HomeSerachController new];
    vc.serachField.placeholder = @"搜索用户名";
    BasseNavigationController * nav = [[BasseNavigationController alloc] initWithRootViewController:vc];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
-(PubulicNavSerachView*)titleView{
    if(!_titleView){
        _titleView=[[PubulicNavSerachView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 35)];
        _titleView.serachField.enabled = NO;
        [_titleView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searcheAction)]];
        kWeakSelf(weakSelf);
        [_titleView.backBtn addActionHandler:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }
    return _titleView;
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
//        _listView.tableFooterView = UIView.new;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;

        [_listView registerClass:[HomeAddFocusCell class] forCellReuseIdentifier:NSStringFromClass([HomeAddFocusCell class])];
        _listView.tableHeaderView = self.headerView;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.backgroundColor = UIColorFromRGB(0xF5F7F5);
        _listView.separatorColor = SeparatorColor;
        _listView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

    }
    return _listView;
}

-(UIView*)headerView{
    if(!_headerView){
        _headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 144)];
        _headerView.backgroundColor = KWhiteColor;
        
        UILabel * topLabl = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 100, 22)];
        topLabl.textColor = UIColorFromRGB(0x273323);
        topLabl.font = SYSTEMFONT(16);
        topLabl.text = @"经常访问";
        
        [_headerView addSubview:topLabl];
        
        NSArray * array = @[@"用户名",@"蜘蛛精",@"运动达人",@"爱斯基摩人"];
        CGFloat left = 16;
        CGFloat spacing = 18;
        CGFloat width = 56;
        for (NSInteger i = 0 ; i<array.count; i++) {
            UIImageView * imageView = UIImageView.new;
            imageView.image = IMAGE_NAMED(@"userIconDefut");
            imageView.layer.cornerRadius = 28;
            imageView.layer.masksToBounds =YES;
            imageView.image = [Utilis getRandomUserImage];
//            imageView.layer.borderWidth = 1;
//            imageView.layer.borderColor = UIColorFromRGB(0x009966).CGColor;
            
            UILabel * label = UILabel.new;
            label.text = array[i];
            label.textColor = UIColorFromRGB(0x273323);
            label.font = SYSTEMFONT(12);
            label.textAlignment = NSTextAlignmentCenter;
            
            [_headerView addSubview:imageView];
            [_headerView addSubview:label];
            
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.height.mas_equalTo(width);
                make.left.mas_equalTo(left + i * width + i * spacing);
                make.top.mas_equalTo(16 + 38);
            }];
            
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(imageView);
                make.top.mas_equalTo(imageView.mas_bottom).offset(10);
            }];
        }
    }
    return _headerView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeAddFocus" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeAddFocusModel *model = [HomeAddFocusModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}

@end



@interface HomeAddFocusCell ()
/**用户头像*/
@property (nonatomic,strong)UIImageView*iconImage;
/**用户名称*/
@property (nonatomic,strong)UILabel*userName;
/**类别*/
@property (nonatomic,strong)UILabel*categoryLb;
/**f数量*/
@property (nonatomic,strong)UILabel*numberLab;
/**按钮*/
@property (nonatomic,strong)UIButton*rightBtn;
@end

@implementation HomeAddFocusCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(HomeAddFocusModel *)model{
    _model = model;
    self.iconImage.image = IMAGE_NAMED(model.userImage);
    self.userName.text = model.userName;
    self.categoryLb.text = model.userType;
    self.numberLab.text = [NSString stringWithFormat:@"发表：%ld  粉丝：%ld",model.fabiaoNum,model.fensiNum];
    self.rightBtn.enabled = !model.isGuan;
}
-(void)createUI{
    [self addSubview:self.iconImage];
    [self addSubview:self.userName];
    [self addSubview:self.categoryLb];
    [self addSubview:self.numberLab];
    [self addSubview:self.rightBtn];
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.width.mas_equalTo(72);
        make.height.mas_equalTo(72);
        make.bottom.mas_equalTo(-16);
        make.top.mas_equalTo(16);
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.iconImage.mas_right).offset(12);
        make.top.mas_equalTo(self.iconImage.mas_top).offset(3);
        
    }];
    [self.categoryLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userName);
        make.centerY.mas_equalTo(self.iconImage.mas_centerY);
    }];
    [self.numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userName);
        make.bottom.mas_equalTo(self.iconImage.mas_bottom).offset(-3);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
}
-(UIImageView*)iconImage{
    if(!_iconImage){
        _iconImage=[UIImageView new];
        _iconImage.image = IMAGE_NAMED(@"userIconDefut");
        _iconImage.layer.cornerRadius = 36;
        _iconImage.layer.masksToBounds = YES;
    }
    return _iconImage;
}
-(UILabel*)userName{
    if(!_userName){
        _userName=[UILabel new];
        _userName.textColor = UIColorFromRGB(0x273323);
        _userName.font = SYSTEMFONT(16);
        _userName.text =@"我我我我是大舌头";
    }
    return _userName;
}
-(UILabel*)categoryLb{
    if(!_categoryLb){
        _categoryLb=[UILabel new];
        _categoryLb.textColor = UIColorFromRGB(0x959E97);
        _categoryLb.font = SYSTEMFONT(14);
        _categoryLb.text =@"知名美食达人";
    }
    return _categoryLb;
}
-(UILabel*)numberLab{
    if(!_numberLab){
        _numberLab=[UILabel new];
        _numberLab.textColor = UIColorFromRGB(0x959E97);
        _numberLab.font = SYSTEMFONT(14);
        _numberLab.text =@"发表：204 粉丝：1234";
    }
    return _numberLab;
}
-(UIButton*)rightBtn{
    if(!_rightBtn){
        _rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_rightBtn setTitle:@"已关注" forState:UIControlStateDisabled];
        [_rightBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
        [_rightBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0xF5F7F5)] forState:UIControlStateDisabled];
        [_rightBtn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
        [_rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateDisabled];
        
        _rightBtn.titleLabel.font = PingFangSC_Regular(14);
        
        kWeakSelf(weakSelf);
        [_rightBtn addActionHandler:^{
            BlockSafeRun(weakSelf.tapBlock);
        }];
        
    }
    return _rightBtn;
}
@end
